package com.example;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class NettyServer {

    public static void main(String[] args) {
        final ServerBootstrap serverBootstrap = new ServerBootstrap();

        final NioEventLoopGroup manager = new NioEventLoopGroup();
        final NioEventLoopGroup worker = new NioEventLoopGroup();
        System.out.println("=====init=====");

        //1.简要启动流程
        serverBootstrap
                .group(manager,worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
                                System.out.println(msg);
                            }
                        });
                    }
                });

        //2.其他方法
        serverBootstrap.handler(new ChannelInitializer<NioServerSocketChannel>() {
            @Override
            protected void initChannel(NioServerSocketChannel nioServerSocketChannel) throws Exception {
                System.out.println("服务启动中");
            }
        });

        serverBootstrap.attr(AttributeKey.newInstance("serverName"),"server");
        /**
         *临时存放已完成三次握手的请求的队列的最大长度，如果连接建立频繁，服务器处理创建新连接较慢，则可以适当调大这个参数
         */
        serverBootstrap.option(ChannelOption.SO_BACKLOG,1024);
        /**
         *心跳
         */
        serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);
        /**
         * 是否开启Nagle算法，true表示关闭，false表示开启。通俗地说，如果要求高实时性，有数据发送时就马上发送，
         * 就设置为关闭；如果需要减少发送次数，减少网络交互，就设置为开启
         */
        serverBootstrap.childOption(ChannelOption.TCP_NODELAY,true);

        //bind 启动
        bind(serverBootstrap,8000);
    }

    private static void bind(final ServerBootstrap serverBootstrap ,int port){
        serverBootstrap.bind(port).addListener((Future<? super Void> future)->{
            if (future.isSuccess()){
                System.out.println("绑定成功,port: "+port);
            }else {
                System.out.println("绑定失败,port: "+port);
                bind(serverBootstrap,port+1);
            }
        });
    }
}
